package com.mazaiting.redeye.module.user.register

import android.Manifest
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.mazaiting.easy.app.IApplicationComponent
import com.mazaiting.easy.base.activity.BaseActivity
import com.mazaiting.easy.base.activity.BaseToolbarActivity
import com.mazaiting.easy.base.interfaces.IPermissionStrategy
import com.mazaiting.easy.base.permission.BasePermissionStrategy
import com.mazaiting.log.L
import com.mazaiting.permission.Permissions
import com.mazaiting.redeye.R
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.base.Config
import com.mazaiting.redeye.bean.UserBean
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.activity_register.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 注册
 */

@Permissions(
    value = [
        Manifest.permission.ACCESS_FINE_LOCATION
    ], code = BaseActivity.REQUEST_PERMISSION_CODE
)
class RegisterActivity : BaseToolbarActivity<RegisterPresenter>(), RegisterContract.View {
    /**
     * 高德定位客户端
     */
    private var mLocationClient: AMapLocationClient? = null
    /**
     * 纬度
     */
    private var mLatitude = 0.0
    /**
     * 精度
     */
    private var mLongitude = 0.0
    /**
     * 地址
     */
    private var mAddress = "中国新疆维吾尔自治区乌鲁木齐市沙依巴克区"
    /**
     * 定位监听，异步获取定位结果
     */
    private val mLocationListener = AMapLocationListener { location ->
        // 判断是否为空
        if (null != location) {
            // 判断异常码, 0 为成功
            if (location.errorCode == 0) {
                L.d(
                    "当前结果来源: " + location.locationType
                            + ", 纬度: " + location.latitude
                            + ", 精度: " + location.longitude
                            + ", 精度信息: " + location.accuracy
                            + ", 定位时间: " + SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(location.time)
                            + ", 地址: " + location.address
                            + ", 国家信息: " + location.country
                            + ", 省信息: " + location.province
                            + ", 城市信息: " + location.city
                            + ", 城区信息: " + location.district
                            + ", 街道信息: " + location.street
                            + ", 街道门牌号: " + location.streetNum
                            + ", 城市编码: " + location.cityCode
                            + ", 地区编码: " + location.adCode
                            + ", 定位点 AOI 信息: " + location.aoiName
                );
                // 精度
                mLongitude = location.longitude
                // 纬度
                mLatitude = location.latitude
                // 地址
                mAddress = location.address
                // 停止定位
//                mLocationClient?.stopLocation()
            } else {
                // 显示错误信息, code为错误码, info是错误信息
                L.d("location Error, ErrCode: " + location.errorCode + ", errInfo: " + location.errorInfo)
            }
        }
    }

    override fun getContentLayout(): Int = R.layout.activity_register

    override fun inject(applicationComponent: IApplicationComponent?) {
        DaggerRegisterComponent
            .builder()
            .applicationComponentImpl(applicationComponent as ApplicationComponentImpl)
            .build()
            .inject(this)
    }

    override fun bindView(view: View?, savedInstanceState: Bundle?) {
        super.bindView(view, savedInstanceState)
        register_acb_register.setOnClickListener { v -> onClick(v.id) }
    }

    /**
     * 按钮点击事件
     */
    private fun onClick(id: Int) {
        when (id) {
            R.id.register_acb_register -> onRegister()
        }
    }

    /**
     * 注册
     */
    private fun onRegister() {
        // 用户名
        val username = register_tiet_username.text.toString()
        // 密码
        val password = register_tiet_password.text.toString()
        presenter?.register(UserBean(username, password, mAddress))
    }

    override fun initData() {
//        val userBean = UserBean("mazaiting6", "123456", mAddress)
//        mPresenter?.register(userBean)
    }

    override fun onResume() {
        super.onResume()
        initAMap()
    }

    /**
     * 初始化高德
     */
    private fun initAMap() {
        // 初始化定位
        mLocationClient = AMapLocationClient(applicationContext)
        // 声明 AMapLocationClientOption
        val option = AMapLocationClientOption()
        // 设置定位监听
        mLocationClient?.setLocationListener(mLocationListener)
        // 设置定位模式
        option.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        // 设置返回地址信息
        option.isNeedAddress = true
        // 设置定位时间间隔
        option.interval = 20000
        // 设置定位参数
        mLocationClient?.setLocationOption(option)
        // 禁止后台定位
        mLocationClient?.disableBackgroundLocation(true)
        // 开始定位
        mLocationClient?.startLocation()
    }

    override fun registerSuccess(msg: String) {
        MobclickAgent.onEvent(this, Config.UM_EVENT_REGISTER)
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        finish()
    }

    override fun registerError(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    override fun onPause() {
        super.onPause()
        if (null != mLocationClient) {
            mLocationClient?.unRegisterLocationListener(mLocationListener)
            mLocationClient = null
        }
    }

    override fun getPermissionStrategy(): IPermissionStrategy = object : BasePermissionStrategy() {
        override fun onSuccess() {
            initAMap()
        }
    }

}
